import requests
from bs4 import BeautifulSoup
from pytickersymbols import PyTickerSymbols
import numpy as np
import pandas as pd
import yfinance as yf
import datetime as dt
import copy
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import plotly.express as px
#Treemap generation for NASDAQ 100
from warnings import simplefilter
simplefilter(action="ignore", category=pd.errors.PerformanceWarning)
stock_data = PyTickerSymbols()
Nasdaq100_yahoo = stock_data.get_nasdaq_100_nyc_yahoo_tickers()
#Nasdaq100_yahoo = ["AAPL","MSFT"]
df=pd.DataFrame(np.nan,index=['recommendationKey','industry','beta','marketCap','Symbols'],
columns=Nasdaq100_yahoo)
for ticker in Nasdaq100_yahoo:
try:
df[ticker].iloc[0]=yf.Ticker(ticker).info['recommendationKey']
df[ticker].iloc[1]=yf.Ticker(ticker).info['industry']
df[ticker].iloc[2]=yf.Ticker(ticker).info['beta']
df[ticker].iloc[3]=yf.Ticker(ticker).info['marketCap']
df[ticker].iloc[4]=ticker
except:
print("Some value is missing")
#yf.Ticker('TCOM').info['recommendationKey']
data=yf.download(Nasdaq100_yahoo,period='1y',interval='1d')['Adj Close']
data.head()
df_T=df.T
return_df = pd.DataFrame()
return_df = (data.iloc[-1]/data.iloc[0])-1
return_df.name = "YTD_Performance"
return_df_df = pd.DataFrame(return_df)
final2=df_T.join(return_df_df)
C:\Users\toram\anaconda3\envs\quant\lib\site-packages\pandas\core\indexing.py:1732: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self._setitem_single_block(indexer, value, name)
Some value is missing Some value is missing [*********************100%***********************] 108 of 108 completed 1 Failed download: - CTRP: No data found, symbol may be delisted
final2.dropna(inplace=True,how="all")
# fillna() on multiple columns with different values
final3 = final2.fillna(value={'recommendationKey':'none','industry':'Trading-Nasdaq','beta':1,'marketCap':1000000000,'Symbols':'AAA','YTD_Performance':0})
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
final3.head(100)
| recommendationKey | industry | beta | marketCap | Symbols | YTD_Performance | |
|---|---|---|---|---|---|---|
| AAPL | buy | Consumer Electronics | 1.203116 | 2935843454976 | AAPL | 0.479000 |
| CSCO | buy | Communication Equipment | 0.954232 | 231258914816 | CSCO | 0.087779 |
| INTC | hold | Semiconductors | 0.545767 | 212497317888 | INTC | -0.170928 |
| MSFT | buy | Software—Infrastructure | 0.910925 | 2365224386560 | MSFT | 0.348380 |
| WBA | hold | Pharmaceutical Retailers | 0.558070 | 41195339776 | WBA | -0.061815 |
| ATVI | buy | Electronic Gaming & Multimedia | 0.544291 | 62728228864 | ATVI | -0.138745 |
| ADBE | buy | Software—Infrastructure | 1.054625 | 219890728960 | ADBE | -0.007777 |
| GOOGL | buy | Internet Content & Information | 1.064960 | 1888858931200 | GOOGL | 0.397618 |
| GOOG | strong_buy | Internet Content & Information | 1.064960 | 1898159800320 | GOOG | 0.396560 |
| AMZN | buy | Internet Retail | 1.114665 | 1730196799488 | AMZN | 0.099209 |
| AMGN | hold | Drug Manufacturers—General | 0.588582 | 134188277760 | AMGN | -0.024430 |
| ADI | buy | Semiconductors | 1.098425 | 89285394432 | ADI | 0.125186 |
| AMAT | buy | Semiconductor Equipment & Materials | 1.411909 | 125181493248 | AMAT | 0.130697 |
| ADSK | buy | Software—Application | 1.459153 | 49086971904 | ADSK | -0.178374 |
| ADP | hold | Staffing & Employment Services | 0.833169 | 94333706240 | ADP | 0.191385 |
| BIDU | buy | Internet Content & Information | 0.953740 | 53738979328 | BIDU | -0.280459 |
| BAIDF | none | Internet Content & Information | 0.953740 | 55151149056 | BAIDF | 0.000000 |
| BIIB | buy | Drug Manufacturers—General | 0.447342 | 31517687808 | BIIB | -0.223395 |
| CERN | hold | Health Information Services | 0.813484 | 27456997376 | CERN | 0.310028 |
| CHTR | buy | Entertainment | 0.960137 | 109675692032 | CHTR | -0.101919 |
| CHKP | hold | Software—Infrastructure | 0.625492 | 18498762752 | CHKP | 0.201337 |
| CTSH | buy | Information Technology Services | 1.103838 | 48703074304 | CTSH | 0.202892 |
| CMCSA | buy | Entertainment | 0.937007 | 216280399872 | CMCSA | -0.118628 |
| COST | buy | Discount Stores | 0.661909 | 252225486848 | COST | 0.609125 |
| CSX | buy | Railroads | 1.220472 | 81990852608 | CSX | 0.181643 |
| TCOM | strong_buy | Travel Services | 1.040354 | 18304057344 | TCOM | -0.364851 |
| DLTR | buy | Discount Stores | 0.734744 | 35805995008 | DLTR | 0.367343 |
| EBAY | buy | Internet Retail | 1.153051 | 35369246720 | EBAY | 0.016766 |
| EA | buy | Electronic Gaming & Multimedia | 0.835137 | 36190461952 | EA | -0.043245 |
| FB | buy | Internet Content & Information | 1.387303 | 627829047296 | FB | -0.210525 |
| FAST | hold | Industrial Distribution | 1.281496 | 33946173440 | FAST | 0.186823 |
| FISV | buy | Information Technology Services | 0.812992 | 67456733184 | FISV | -0.150671 |
| GILD | buy | Drug Manufacturers—General | 0.424212 | 75899944960 | GILD | -0.046185 |
| ILMN | buy | Diagnostics & Research | 0.923228 | 56116510720 | ILMN | -0.097316 |
| INCY | buy | Biotechnology | 0.692421 | 17661734912 | INCY | -0.017756 |
| INTU | buy | Software—Application | 1.132381 | 137563996160 | INTU | 0.303246 |
| ISRG | buy | Medical Instruments & Supplies | 1.169783 | 108318842880 | ISRG | 0.250432 |
| JD | buy | Internet Retail | 0.693897 | 97889099776 | JD | -0.234100 |
| JDCMF | none | Internet Retail | 0.693897 | 94445412352 | JDCMF | -0.225115 |
| KHC | hold | Packaged Foods | 0.988188 | 48168800256 | KHC | 0.006445 |
| LRCX | buy | Semiconductor Equipment & Materials | 1.261318 | 79488491520 | LRCX | 0.006710 |
| MAR | buy | Lodging | 1.687500 | 58514653184 | MAR | 0.235072 |
| MCHP | buy | Semiconductors | 1.562007 | 44407001088 | MCHP | 0.068737 |
| MU | buy | Semiconductors | 1.169291 | 92157894656 | MU | -0.051021 |
| MDLZ | buy | Confectioners | 0.724409 | 87339835392 | MDLZ | 0.069031 |
| MNST | buy | Beverages—Non-Alcoholic | 1.149114 | 43719757824 | MNST | -0.102752 |
| NTES | buy | Electronic Gaming & Multimedia | 0.510826 | 63599157248 | NTES | -0.081108 |
| NETTF | none | Electronic Gaming & Multimedia | 0.510826 | 64182345728 | NETTF | -0.111571 |
| NFLX | buy | Entertainment | 0.986220 | 176755507200 | NFLX | -0.239080 |
| NVDA | buy | Semiconductors | 1.411417 | 715752472576 | NVDA | 1.207283 |
| NXPI | buy | Semiconductors | 1.408956 | 51103019008 | NXPI | 0.011170 |
| ORLY | buy | Specialty Retail | 1.020177 | 46835081216 | ORLY | 0.379820 |
| PCAR | buy | Farm & Heavy Construction Machinery | 0.939960 | 31560200192 | PCAR | 0.005921 |
| PAYX | hold | Staffing & Employment Services | 0.964566 | 47764221952 | PAYX | 0.352906 |
| PYPL | buy | Credit Services | 1.292322 | 144168517632 | PYPL | -0.486316 |
| QCOM | buy | Semiconductors | 1.258366 | 181086355456 | QCOM | 0.244974 |
| REGN | buy | Biotechnology | 0.184547 | 75563220992 | REGN | 0.448351 |
| ROST | buy | Apparel Retail | 1.082677 | 33806612480 | ROST | -0.195642 |
| SIRI | buy | Broadcasting | 0.955708 | 26174777344 | SIRI | 0.124519 |
| SWKS | buy | Semiconductors | 1.106791 | 22636365824 | SWKS | -0.210886 |
| SBUX | buy | Restaurants | 0.901082 | 104966717440 | SBUX | -0.151522 |
| TMUS | buy | Telecom Services | 0.502460 | 160383860736 | TMUS | 0.038717 |
| TSLA | buy | Auto Manufacturers | 2.048720 | 1141188853760 | TSLA | 0.794337 |
| TXN | buy | Semiconductors | 0.934055 | 176314368000 | TXN | 0.050632 |
| VRSK | buy | Consulting Services | 0.846948 | 34675040256 | VRSK | 0.199504 |
| VRTX | buy | Biotechnology | 0.573818 | 65342279680 | VRTX | 0.185686 |
| BKNG | buy | Travel Services | 1.207677 | 97835712512 | BKNG | 0.007077 |
| EXC | buy | Utilities—Diversified | 0.570374 | 45924319232 | EXC | 0.566310 |
| HON | buy | Conglomerates | 1.151574 | 134941745152 | HON | -0.083414 |
| AMD | buy | Semiconductors | 1.848425 | 200328019968 | AMD | 0.591392 |
| ALGN | buy | Medical Devices | 1.640255 | 35637628928 | ALGN | -0.121318 |
| AEP | buy | Utilities—Regulated Electric | 0.335629 | 49713016832 | AEP | 0.190503 |
| ANSS | hold | Software—Application | 1.273129 | 28622780416 | ANSS | -0.043187 |
| AVGO | buy | Semiconductors | 1.019192 | 261405999104 | AVGO | 0.392361 |
| CDNS | buy | Software—Application | 1.123031 | 46257737728 | CDNS | 0.249266 |
| CTAS | buy | Specialty Business Services | 1.497047 | 44323598336 | CTAS | 0.276973 |
| CPRT | buy | Auto & Truck Dealerships | 1.127460 | 31024232448 | CPRT | 0.229457 |
| FOXA | buy | Broadcasting | 0.961391 | 22081841152 | FOXA | 0.104774 |
| FXBY | none | Asset Management | 0.984251 | 8034426 | FXBY | 0.000000 |
| FOX | none | Broadcasting | 0.961391 | 22099783680 | FOX | 0.058128 |
| IDXX | buy | Diagnostics & Research | 1.002952 | 46468014080 | IDXX | 0.149130 |
| KLAC | buy | Semiconductor Equipment & Materials | 1.256397 | 57821810688 | KLAC | 0.232761 |
| SNPS | buy | Software—Infrastructure | 1.166338 | 52300148736 | SNPS | 0.416930 |
| VRSN | buy | Software—Infrastructure | 0.911909 | 24389871616 | VRSN | 0.099066 |
| ASMLF | none | Semiconductor Equipment & Materials | 0.981876 | 289445806080 | ASMLF | 0.164773 |
| ASML | buy | Semiconductor Equipment & Materials | 0.981876 | 286221828096 | ASML | 0.173037 |
| CDW | buy | Information Technology Services | 1.115157 | 24412043264 | CDW | 0.104515 |
| DXCM | strong_buy | Medical Devices | 0.889763 | 49986842624 | DXCM | 0.473824 |
| PEP | buy | Beverages—Non-Alcoholic | 0.653051 | 232316846080 | PEP | 0.192440 |
| XEL | buy | Utilities—Regulated Electric | 0.354822 | 39047356416 | XEL | 0.101230 |
| TEAM | buy | Software—Application | 0.950295 | 79243837440 | TEAM | 0.526297 |
| DOCU | buy | Software—Application | 0.924769 | 22081935360 | DOCU | -0.438576 |
| KDP | buy | Beverages—Non-Alcoholic | 0.610985 | 54180806656 | KDP | 0.099378 |
| LULU | buy | Apparel Retail | 1.342027 | 44205764608 | LULU | 0.090002 |
| MRVL | buy | Semiconductors | 1.197834 | 64504860672 | MRVL | 0.638081 |
| MTCH | buy | Internet Content & Information | 1.000000 | 31736973312 | MTCH | -0.166053 |
| MELI | buy | Internet Retail | 1.511318 | 63599538176 | MELI | -0.104349 |
| MRNA | buy | Biotechnology | 1.546391 | 72807579648 | MRNA | 0.462405 |
| OKTA | buy | Software—Infrastructure | 1.080952 | 24593436672 | OKTA | -0.274210 |
| PTON | buy | Leisure | 0.997116 | 10355867648 | PTON | -0.703771 |
fig = px.treemap(final3.loc[final3["beta"].ge(0)],path=[px.Constant("NASDAQ100-Index"),'Symbols'],values="beta",color="YTD_Performance")
fig.show()
fig = px.treemap(final3.loc[final3["beta"].ge(0)],path=[px.Constant("NASDAQ100-Index-by-Beta-Recommendations as per Yahoofinance"),'recommendationKey','industry','Symbols'],values="beta",color="YTD_Performance")
fig.show()
fig = px.treemap(final3.loc[final3["beta"].ge(0)],path=[px.Constant("NASDAQ100-Index-by-MarketCap"),'recommendationKey','industry','Symbols'],values="marketCap",color="YTD_Performance")
fig.show()
#Stock screening for Most actively traded stocks in NASDAQ.
url_volume = ("https://www.marketwatch.com/tools/screener/market?exchange=nasdaq&subreport=mostactive")
page = requests.get(url_volume)
soup_volume = BeautifulSoup(page.text, 'lxml')
volume = soup_volume.findAll('div', class_= 'cell__content fixed--cell')
most_active_tickers = []
for div_tag in soup_volume.findAll('div', class_= 'cell__content fixed--cell'):
most_active_tickers.append(div_tag.text)
most_active_tickers.pop(0)
most_active_tickers
['HYMC', 'SXTC', 'SNDL', 'CLVR', 'HOOD', 'TQQQ', 'AMD', 'SQQQ', 'TLRY', 'AAPL', 'GFAI', 'MULN', 'SOFI', 'QQQ', 'VEON', 'AAL', 'NVDA', 'IGMS', 'BBIG', 'FAMI', 'LCID', 'MU', 'IMPP', 'FB', 'MVIS']
ma_df=pd.DataFrame(np.nan,index=['recommendationKey','industry','beta','marketCap','Symbols','averageVolume'],
columns=most_active_tickers)
for ticker in most_active_tickers:
try:
ma_df[ticker].iloc[0]=yf.Ticker(ticker).info['recommendationKey']
ma_df[ticker].iloc[1]=yf.Ticker(ticker).info['industry']
ma_df[ticker].iloc[2]=yf.Ticker(ticker).info['beta']
ma_df[ticker].iloc[3]=yf.Ticker(ticker).info['marketCap']
ma_df[ticker].iloc[4]=ticker
ma_df[ticker].iloc[5]=yf.Ticker(ticker).info['averageVolume']
except:
print("Some value is missing")
ma_data=yf.download(most_active_tickers,period='1y',interval='1d')['Adj Close']
ma_data.head()
ma_df_T=ma_df.T
ma_return_df = pd.DataFrame()
ma_return_df = (ma_data.iloc[-1]/ma_data.iloc[0])-1
ma_return_df.name = "MostActive_YTD_Performance"
ma_return_df_df = pd.DataFrame(ma_return_df)
ma_final2=ma_df_T.join(ma_return_df_df)
C:\Users\toram\anaconda3\envs\quant\lib\site-packages\pandas\core\indexing.py:1732: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
Some value is missing Some value is missing Some value is missing [*********************100%***********************] 25 of 25 completed
ma_final2.dropna(inplace=True,how="all")
ma_final2
| recommendationKey | industry | beta | marketCap | Symbols | averageVolume | MostActive_YTD_Performance | |
|---|---|---|---|---|---|---|---|
| HYMC | buy | Gold | 0.321078 | 279922496 | HYMC | 35092585 | -0.264205 |
| SXTC | none | Drug Manufacturers—Specialty & Generic | 0.240225 | 8950218 | SXTC | 6601649 | -0.842097 |
| SNDL | hold | Drug Manufacturers—Specialty & Generic | 5.238538 | 1516548352 | SNDL | 91795298 | -0.363947 |
| CLVR | buy | Drug Manufacturers—Specialty & Generic | 0.510278 | 84192112 | CLVR | 3362934 | -0.772302 |
| HOOD | hold | Software—Infrastructure | None | 13316482048 | HOOD | 22483909 | NaN |
| TQQQ | NaN | NaN | NaN | NaN | NaN | NaN | 0.432791 |
| AMD | buy | Semiconductors | 1.848425 | 199595704320 | AMD | 100743022 | 0.621447 |
| SQQQ | NaN | NaN | NaN | NaN | NaN | NaN | -0.543709 |
| TLRY | hold | Drug Manufacturers—Specialty & Generic | None | 4002027264 | TLRY | 28974606 | -0.622505 |
| AAPL | buy | Consumer Electronics | 1.185531 | 2918724665344 | AAPL | 94158368 | 0.501448 |
| GFAI | buy | Security & Protection Services | None | 41207344 | GFAI | 10721878 | NaN |
| MULN | none | Auto Manufacturers | None | 81240848 | MULN | 102878332 | -0.780784 |
| SOFI | buy | Credit Services | None | 8104250368 | SOFI | 64742709 | -0.385455 |
| QQQ | NaN | NaN | NaN | NaN | NaN | NaN | 0.186588 |
| VEON | buy | Telecom Services | 1.557578 | 1834878464 | VEON | 32301342 | -0.592959 |
| AAL | hold | Airlines | 1.595472 | 11785500672 | AAL | 44455550 | -0.247098 |
| NVDA | buy | Semiconductors | 1.411417 | 713534603264 | NVDA | 52568696 | 1.227826 |
| IGMS | buy | Biotechnology | -0.841384 | 946258560 | IGMS | 312481 | -0.607114 |
| BBIG | none | Leisure | 0.303172 | 624756992 | BBIG | 39473619 | 0.325490 |
| FAMI | none | Packaged Foods | -0.174217 | 96661016 | FAMI | 32349147 | -0.848972 |
| LCID | buy | Auto Manufacturers | None | 45150531584 | LCID | 33635178 | 0.163760 |
| MU | buy | Semiconductors | 1.169291 | 91827552256 | MU | 22220868 | -0.049546 |
| IMPP | none | Oil & Gas E&P | None | 92710928 | IMPP | 37928904 | NaN |
| FB | buy | Internet Content & Information | 1.387303 | 625528995840 | FB | 36568319 | -0.201875 |
| MVIS | hold | Scientific & Technical Instruments | 3.423228 | 913696512 | MVIS | 5551718 | -0.549147 |
# fillna() on multiple columns with different values
ma_final3 = ma_final2.fillna(value={'recommendationKey':'none','industry':'Trading-Nasdaq','beta':1,'marketCap':1000000000,'Symbols':'AAA','MostActive_YTD_Performance':0.01,'averageVolume':10000000})
ma_final3
| recommendationKey | industry | beta | marketCap | Symbols | averageVolume | MostActive_YTD_Performance | |
|---|---|---|---|---|---|---|---|
| HYMC | buy | Gold | 0.321078 | 279922496 | HYMC | 35092585 | -0.264205 |
| SXTC | none | Drug Manufacturers—Specialty & Generic | 0.240225 | 8950218 | SXTC | 6601649 | -0.842097 |
| SNDL | hold | Drug Manufacturers—Specialty & Generic | 5.238538 | 1516548352 | SNDL | 91795298 | -0.363947 |
| CLVR | buy | Drug Manufacturers—Specialty & Generic | 0.510278 | 84192112 | CLVR | 3362934 | -0.772302 |
| HOOD | hold | Software—Infrastructure | 1.000000 | 13316482048 | HOOD | 22483909 | 0.010000 |
| TQQQ | none | Trading-Nasdaq | 1.000000 | 1000000000 | AAA | 10000000 | 0.432791 |
| AMD | buy | Semiconductors | 1.848425 | 199595704320 | AMD | 100743022 | 0.621447 |
| SQQQ | none | Trading-Nasdaq | 1.000000 | 1000000000 | AAA | 10000000 | -0.543709 |
| TLRY | hold | Drug Manufacturers—Specialty & Generic | 1.000000 | 4002027264 | TLRY | 28974606 | -0.622505 |
| AAPL | buy | Consumer Electronics | 1.185531 | 2918724665344 | AAPL | 94158368 | 0.501448 |
| GFAI | buy | Security & Protection Services | 1.000000 | 41207344 | GFAI | 10721878 | 0.010000 |
| MULN | none | Auto Manufacturers | 1.000000 | 81240848 | MULN | 102878332 | -0.780784 |
| SOFI | buy | Credit Services | 1.000000 | 8104250368 | SOFI | 64742709 | -0.385455 |
| QQQ | none | Trading-Nasdaq | 1.000000 | 1000000000 | AAA | 10000000 | 0.186588 |
| VEON | buy | Telecom Services | 1.557578 | 1834878464 | VEON | 32301342 | -0.592959 |
| AAL | hold | Airlines | 1.595472 | 11785500672 | AAL | 44455550 | -0.247098 |
| NVDA | buy | Semiconductors | 1.411417 | 713534603264 | NVDA | 52568696 | 1.227826 |
| IGMS | buy | Biotechnology | -0.841384 | 946258560 | IGMS | 312481 | -0.607114 |
| BBIG | none | Leisure | 0.303172 | 624756992 | BBIG | 39473619 | 0.325490 |
| FAMI | none | Packaged Foods | -0.174217 | 96661016 | FAMI | 32349147 | -0.848972 |
| LCID | buy | Auto Manufacturers | 1.000000 | 45150531584 | LCID | 33635178 | 0.163760 |
| MU | buy | Semiconductors | 1.169291 | 91827552256 | MU | 22220868 | -0.049546 |
| IMPP | none | Oil & Gas E&P | 1.000000 | 92710928 | IMPP | 37928904 | 0.010000 |
| FB | buy | Internet Content & Information | 1.387303 | 625528995840 | FB | 36568319 | -0.201875 |
| MVIS | hold | Scientific & Technical Instruments | 3.423228 | 913696512 | MVIS | 5551718 | -0.549147 |
fig = px.treemap(ma_final3.loc[ma_final3["beta"].ge(0)],path=[px.Constant("Most-Active-NASDAQ-avgVolume-Recommendation-mentioned-in-Yahoo"),'recommendationKey','industry','Symbols'],values="averageVolume",color="MostActive_YTD_Performance")
fig.show()